package com.final_project.zombiemenace;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

public class MusicManager {
	static MediaPlayer mediaplayer;
	private static final String TAG = "MusicManager";
	static final int MUSIC_PREVIOUS = -1;
	private static int currentMusic = -1;
	private static int previousMusic = -1;

	public static void start(Context context, int music) {
		start(context, music, false);
	}

	public static void start(Context context, int music, boolean force) {
		if (!force && currentMusic > -1) {
			// already playing some music and not forced to change
			return;
		}

		if (music == MUSIC_PREVIOUS) {
			Log.d(TAG, "Using previous music [" + previousMusic + "]");
			music = previousMusic;
		}
		if (currentMusic == music) {
			// already playing this music
			return;
		}
		if (currentMusic != -1) {
			previousMusic = currentMusic;
			Log.d(TAG, "Previous music was [" + previousMusic + "]");
			// playing some other music, pause it and change
			pause();
		}
		currentMusic = music;
		Log.d(TAG, "Current music is now [" + currentMusic + "]");
		if (mediaplayer != null) {
			if (!mediaplayer.isPlaying()) {
				mediaplayer.start();
			}
		} else {
			// create main menu music
			mediaplayer = MediaPlayer.create(context,
					R.raw.dst_the_haunted_chapel);
		}

		if (mediaplayer == null) {
			Log.e(TAG, "player was not created successfully");
		} else {
			try {
				mediaplayer.setLooping(true);
				mediaplayer.start();
			} catch (Exception e) {
				Log.e(TAG, e.getMessage(), e);
			}
		}
	}

	public static void pause() {
		if (mediaplayer != null) {
			if (mediaplayer.isPlaying()) {
				mediaplayer.pause();
			}
		}

		// previousMusic should always be something valid
		if (currentMusic != -1) {
			{
				previousMusic = currentMusic;
				Log.d(TAG, "Previous music was [" + previousMusic + "]");
			}
			currentMusic = -1;
			Log.d(TAG, "Current music is now [" + currentMusic + "]");
		}
	}

	public static void release() {
		Log.d(TAG, "Releasing media players");
		try {
			if (mediaplayer != null) {
				if (mediaplayer.isPlaying()) {
					mediaplayer.stop();
				}
				mediaplayer.release();
			}
		} catch (Exception e) {
			Log.e(TAG, e.getMessage(), e);
		}

		if (currentMusic != -1) {
			previousMusic = currentMusic;
			Log.d(TAG, "Previous music was [" + previousMusic + "]");
		}
		currentMusic = -1;
		Log.d(TAG, "Current music is now [" + currentMusic + "]");
	}
}
